package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.vo.bookListVo;
import com.example.tianling.common.vo.materialCollectionListVo;
import com.example.tianling.dao.MaterialCollectionsMapper;
import com.example.tianling.dao.UserBookMarksMapper;
import com.example.tianling.entity.*;
import com.example.tianling.service.MaterialCollectionsService;
import com.example.tianling.service.UserBookMarksService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MaterialCollectionsServiceImpl
        extends ServiceImpl<MaterialCollectionsMapper, MaterialCollectionsEntity>
        implements MaterialCollectionsService {
    @Autowired
    private MaterialCollectionsMapper materialCollectionsMapper;
    @Override
    public Integer setMaterialCollection(MaterialCollectionsEntity materialCollectionsEntity, Integer userId) throws ParseException {
        QueryWrapper<MaterialCollectionsEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        System.out.println(materialCollectionsEntity.getMaterial_id());
        materialCollectionsEntityQueryWrapper.eq("material_id",materialCollectionsEntity.getMaterial_id());
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        if(materialCollectionsMapper.selectOne(materialCollectionsEntityQueryWrapper)!=null){
            return -2;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateTimeString = formatter.format(new Date(System.currentTimeMillis()));

        materialCollectionsEntity.setCollectionTime(formatter.parse(currentDateTimeString));
        materialCollectionsEntity.setUserId(userId);
        return materialCollectionsMapper.insert(materialCollectionsEntity);
    }

    @Override
    public List<materialCollectionListVo> getMyMaterialCollections(Integer userId, Integer pageNum){
        Page<materialCollectionListVo> pg = new Page<>(pageNum, 10);

        IPage<materialCollectionListVo> materialCollectionListVoIPage = materialCollectionsMapper.selectJoinPage(pg,materialCollectionListVo.class,
                new MPJLambdaWrapper<materialCollectionListVo>()
                        .select(MaterialEntity::getId)
                        .select(MaterialEntity::getTitle)
                        .select(MaterialEntity::getIsNeedVip)
                        .select(MaterialEntity::getContent)
                        .eq(MaterialCollectionsEntity::getUserId,userId)
                        .leftJoin(MaterialEntity.class,MaterialEntity::getId,MaterialCollectionsEntity::getMaterial_id)
                        .orderByDesc(MaterialCollectionsEntity::getCollectionTime));
        for(materialCollectionListVo materialEntity :materialCollectionListVoIPage.getRecords()){
            materialEntity.setContent(materialEntity.getContent().substring(0,20));
        }
        return materialCollectionListVoIPage.getRecords();
    }
    @Override
    public Integer cancelMyMaterialCollection(Integer userId ,Integer materialId){
        QueryWrapper<MaterialCollectionsEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        materialCollectionsEntityQueryWrapper.eq("material_id",materialId);
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        return materialCollectionsMapper.delete(materialCollectionsEntityQueryWrapper);
    }

    @Override
    public Integer isCollectMaterial(Integer userId, Integer materialId) {
        QueryWrapper<MaterialCollectionsEntity> materialCollectionsEntityQueryWrapper = new QueryWrapper<>();
        materialCollectionsEntityQueryWrapper.eq("material_id",materialId);
        materialCollectionsEntityQueryWrapper.eq("user_id",userId);
        if(materialCollectionsMapper.selectCount(materialCollectionsEntityQueryWrapper)==0){
            return 0;
        }
        else{
            return 1;
        }
    }
}
